
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
class UserBlogStatistics:
    def __init__(self, blog_list):
        self.blog_list = blog_list
        self.statistics()
    
    def statistics(self):
        # 统计一年前到现在的博客赞同、转发、评论数
        # 计算时间点 并转化成 yyyy-mm-dd hh-mm-ss的格式
        now = datetime.now()
        one_week_ago = (now - timedelta(weeks=1)).strftime("%Y-%m-%d %H-%M-%S")
        one_month_ago = (now - relativedelta(months=1)).strftime("%Y-%m-%d %H-%M-%S")
        
        
        # 点赞数
        self.likes_week_ago = 0
        self.likes_month_ago = 0

        # 转发数
        self.reposts_week_ago = 0
        self.reposts_month_ago = 0

        # 评论数
        self.comments_week_ago = 0
        self.comments_month_ago = 0
        
        for blog in self.blog_list:
            # blog.times 时间表示为 yyyy-mm-dd hh:mm:ss
            if blog.times > one_month_ago:
                self.likes_month_ago += blog.attitudes
                self.reposts_month_ago += blog.reposts
                self.comments_month_ago += blog.comments
            if blog.times > one_week_ago:
                self.likes_week_ago += blog.attitudes
                self.reposts_week_ago += blog.reposts
                self.comments_week_ago += blog.comments
    
    def json(self):
        return {
            "blog_list": [blog.json() for blog in self.blog_list],
            "week_ago": {
                "likes": self.likes_week_ago,
                "reposts": self.reposts_week_ago,
                "comments": self.comments_week_ago
            },
            "month_ago": {
                "likes": self.likes_month_ago,
                "reposts": self.reposts_month_ago,
                "comments": self.comments_month_ago
            },
        }
            
            